package sskj.regulatory.presentation.screen.main

import android.annotation.SuppressLint
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.content.ContextCompat
import android.support.v4.view.ViewPager
import com.qmuiteam.qmui.util.QMUIResHelper
import com.qmuiteam.qmui.widget.QMUITabSegment
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import kotlinx.android.synthetic.main.fragment_main.view.*
import sskj.regulatory.R
import sskj.regulatory.presentation.base_mvp.base.BaseFragment
import sskj.regulatory.presentation.screen.home.HomeFragment
import sskj.regulatory.presentation.screen.setup.SetupFragment
import java.util.ArrayList
import javax.inject.Inject

class MainFragment : BaseFragment<MainContract.View, MainContract.Presenter>(), MainContract.View {

    @Inject
    lateinit var mPresenter: MainPresenter

    private var mCurrentViewPagerName: String? = null

    private var mChannelNames = ArrayList<String>()

    private var mFragmentList = ArrayList<Fragment>()

    override fun onFetchTransitionConfig(): TransitionConfig {
        return SCALE_TRANSITION_CONFIG
    }

    private val currentViewPagerPosition: Int
        get() {
            var position = 0
            if (mCurrentViewPagerName != null) {
                for (i in mChannelNames.indices) {
                    if (mCurrentViewPagerName == mChannelNames[i]) {
                        position = i
                    }
                }
            }
            return position
        }

    override fun onBackPressed(): Boolean = false

    override fun initPresenter(): MainContract.Presenter = mPresenter

    override fun injectDependencies() {
        activityComponent.inject(this)
    }

    override val layoutResId: Int = R.layout.fragment_main


    override fun initView() {
        initTab()
        initTabAndPage()
    }

    @SuppressLint("ResourceType")
    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden) { //返回的时候

            var fragment: SetupFragment? = childFragmentManager.fragments.filter {
                it::class.simpleName == SetupFragment::class.simpleName
            }.singleOrNull() as SetupFragment

            fragment?.refresh()

        }
    }

    override fun showError(message: String?) {
        QMUIDialog.MessageDialogBuilder(getActivity())
                .setTitle("错误提示")
                .setMessage(message)
                .addAction("确定") { dialog, _ -> dialog.dismiss() }
                .create(R.style.QMUI_Dialog).show()
    }

    private fun initTabAndPage() {

        mFragmentList.clear()
        mChannelNames.clear()

        mChannelNames.add(getString(R.string.home))
        mFragmentList.add(HomeFragment())


        mChannelNames.add(getString(R.string.setup))
        mFragmentList.add(SetupFragment())

        mCacheView.mainViewPager.offscreenPageLimit = 1

        val adapter = MainPagerAdapter(
                childFragmentManager, mFragmentList, mChannelNames)

        mCacheView.mainViewPager.setNoScroll(true)

        mCacheView.mainViewPager.adapter = adapter//给ViewPager设置适配器

        mCacheView.mainTabs.setupWithViewPager(mCacheView.mainViewPager, false)

        mCacheView.mainViewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                mCurrentViewPagerName = mChannelNames[position]
            }

            override fun onPageScrollStateChanged(state: Int) {

            }
        })


        val currentViewPagerPosition = currentViewPagerPosition
        mCacheView.mainViewPager.setCurrentItem(currentViewPagerPosition, false)
    }

    private fun initTab() {

        val normalColor = QMUIResHelper.getAttrColor(context, R.attr.qmui_config_color_gray_6)
        val selectColor = QMUIResHelper.getAttrColor(context, R.attr.qmui_config_color_blue)

        mCacheView.mainTabs.setDefaultNormalColor(normalColor)

        mCacheView.mainTabs.setDefaultSelectedColor(selectColor)

        val component = QMUITabSegment.Tab(
                ContextCompat.getDrawable(context!!, R.mipmap.icon_tabbar_component),
                ContextCompat.getDrawable(context!!, R.mipmap.icon_tabbar_component_selected),
                "首页", false
        )


        val lab = QMUITabSegment.Tab(
                ContextCompat.getDrawable(context!!, R.mipmap.icon_tabbar_lab),
                ContextCompat.getDrawable(context!!, R.mipmap.icon_tabbar_lab_selected),
                "我的", false
        )

        mCacheView.mainTabs.addTab(component).addTab(lab)
    }


    class MainPagerAdapter(fm: FragmentManager, private val mFragments: List<Fragment>, private val mTitles: List<String>) : FragmentPagerAdapter(fm) {

        override fun getItem(position: Int): Fragment {
            return mFragments[position]
        }

        override fun getCount(): Int {
            return mFragments.size
        }

        override fun getPageTitle(position: Int): CharSequence? {
            return mTitles[position]
        }

    }

    override fun canDragBack(): Boolean {
        return false
    }

}